package com.crane.common.core.dto;


import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.ErrorCode;

import java.io.Serializable;
import java.text.MessageFormat;

/**
 * @author zhenxing.pi
 */
public class Response implements Serializable {

    private static final long serialVersionUID = 1L;

    private boolean isSuccess;

    private String code;

    private String errMsg;


    public static Response buildFailure(String errMessage) {
        Response response = new Response();
        response.setSuccess(false);
        response.setCode(BasicErrorCode.SYS_ERROR.getErrCode());
        response.setErrMsg(errMessage);
        return response;
    }


    public static Response buildFailure(String errCode, String errMessage) {
        Response response = new Response();
        response.setSuccess(false);
        response.setCode(errCode);
        response.setErrMsg(errMessage);
        return response;
    }

    public static Response buildFailure(ErrorCode errorCode) {
        Response response = new Response();
        response.setSuccess(false);
        response.setCode(errorCode.getErrCode());
        response.setErrMsg(errorCode.getErrDesc());
        return response;
    }

    public static Response buildFailure(ErrorCode errorCode, Object... replacements) {
        Response response = new Response();
        response.setSuccess(false);
        response.setCode(errorCode.getErrCode());
        response.setErrMsg(MessageFormat.format(errorCode.getErrDesc(), replacements));
        return response;
    }

    public static Response buildSuccess() {
        Response response = new Response();
        /**
         * 兼容以前格式
         */
        response.setCode(BasicErrorCode.SERVER_SUCCESS.getErrCode());
        response.setSuccess(true);
        return response;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }
}
